package com.example.exchange;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.poi.ss.formula.functions.T;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.example.exchange.DOMUtils;
import com.example.exchange.XMLObject;

import net.sf.json.JSONArray;

/**
 * 
 * Copyright ©2019 KANGPING Info. All rights reserved.
 * 
 * Read and create Unifier XML
 * Project NAME：primavera.integration   
 * Class NAME：ServiceUtil 
 * Author：Kangping Zhou
 * Date：Aug 14, 2019 1:10:44 PM      
 * Description：kangpingchn@gmail.com
 * Vesion: v1.0.0
 *
 */
public class ServiceUtil {
	static Logger logger = Logger.getLogger(ServiceUtil.class);

	/**
	 * 
	 * @param xmlobject
	 * @return
	 * @throws DocumentException
	 */
	public static List<T> convertbean(String XmlRootNM, Class clazz, XMLObject xmlobject) throws DocumentException {
		return MapUtils.map2Java(clazz, readPUXML(XmlRootNM, xmlobject));
	}

	/**
	 * readPUXML exchange to entity list
	 * @param object
	 * @return
	 * @throws DocumentException
	 */
	public static List<Map<String, String>> readPUXML(String XmlRootNM, XMLObject object) throws DocumentException {
		List<Map<String, String>> listbpmap = new ArrayList<Map<String, String>>();
		Map<String, String> bpmap = null;

		Document document = DOMUtils.getXMLByString(object.getXmlcontents());
		// select root=list_wrapper
		Element root = document.getRootElement();
		List<Element> bplist = root.elements(XmlRootNM);

		for (Element bp : bplist) {
			bpmap = new HashMap<String, String>();
			Iterator<Element> bpiterator = bp.elementIterator();
			while (bpiterator.hasNext()) {
				Element bp_property = (Element) bpiterator.next();
				bpmap.put(bp_property.getName(), bp_property.getText());
			}
			listbpmap.add(bpmap);
		}
		return listbpmap;
	}

	/**
	 * 
	 * @param object
	 * @return
	 * @throws DocumentException
	 */
	public static List<Map<String, String>> readPUChildXML(String XmlRootNM, XMLObject object) throws DocumentException {
		List<Map<String, String>> listbpmap = new ArrayList<Map<String, String>>();
		Map<String, String> bpmap = null;

		Document document = DOMUtils.getXMLByString(object.getXmlcontents());
		// select root=list_wrapper
		Element root = document.getRootElement();
		List<Element> bplist = root.elements(XmlRootNM);

		List<Element> list_bplineitem = null, list_lineitem_cost = null;

		// System.out.println("bpsize: "+bplist.size());
		for (Element bp : bplist) {
			bpmap = new HashMap<String, String>();
			Iterator<Element> bpiterator = bp.elementIterator();
			while (bpiterator.hasNext()) {
				Element bp_property = (Element) bpiterator.next();
				if (!(bp_property.getName().equals("_bp_lineitems") || bp_property.getName().equals("_cost_allocation"))) {
					bpmap.put(bp_property.getName(), bp_property.getText());
				} else {
					break;
				}
			}
			listbpmap.add(bpmap);

			// the detail need click into to check , unnessory to check.
			list_bplineitem = bp.elements("_bp_lineitems");
			for (Element item : list_bplineitem) {
				// System.out.println("uuu_tab_id: "+item.element("uuu_tab_id").getText());
				list_lineitem_cost = item.elements("_cost_allocation");
				for (Element cost : list_lineitem_cost) {
					logger.info("short_desc: " + cost.element("short_desc").getText());
				}
			}
		}
		return listbpmap;
	}

	/**
	 * @Title: XMLExchange 
	 * @Description: TODO
	 * @param @param param
	 * @param @param obs
	 * @param @return
	 * @param @throws DocumentException 
	 * @return String
	 * @throws
	 */
	public static String XMLExchange(String param, String obs) throws DocumentException {
		Document document = DOMUtils.getXMLByString(param);
		// select root=list_wrapper
		Element root = document.getRootElement();
		List<Element> bplist = root.elements(XMLHeaderConstants.BP);

		for (Element bp : bplist) {
			Iterator<Element> bpiterator = bp.elementIterator();
			while (bpiterator.hasNext()) {
				Element bp_property = (Element) bpiterator.next();

				if (bp_property.getName() == "uuu_creation_date") {
					bp.remove(bp_property);
				}
				if (bp_property.getName() == "creator_id") {
					bp_property.setText(obs);
				}
				if (bp_property.getName() == "status") {
					bp_property.setText("Pending");
				}

				if (bp_property.getName() == "trigger_status") {
					bp_property.setText("");
				}
				if (bp_property.getName() == "sum_status") {
					bp_property.setText("");
				}
			}
		}

		return convertDocToXMLStr(false, document);
	}

	/**
	 * Demo how to create new cdata xml
	 * @param length
	 * @return
	 */
	public static String createXML(boolean iscdate, String XmlRootNM, Class clazz, Map objmap) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = itemsElement.addElement(XmlRootNM);
		Element element = null;
		BeanInfo beaninfo = null;
		try {
			beaninfo = Introspector.getBeanInfo(clazz);
		} catch (IntrospectionException e1) {
			e1.printStackTrace();
		}

		PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
		if (propertyDescriptors != null && propertyDescriptors.length > 0) {
			for (PropertyDescriptor pd : propertyDescriptors) {
				if (!pd.getName().equalsIgnoreCase("class") || !pd.getName().equalsIgnoreCase("project_Id")) {
					element = itemElement.addElement(pd.getName());
					Object obj = objmap.get(pd.getName());
					obj = obj == null ? "" : obj;
					element.setText(obj.toString());
				}
			}
		}
		return convertDocToXMLStr(iscdate, document);
	}
	
	/**
	 * Demo how to create new cdata xml
	 * @param length
	 * @return
	 */
	public static String createXMList(boolean iscdate, String XmlRootNM, Class clazz, List<Map> objmaps) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = null;
		Element element = null;
		BeanInfo beaninfo = null;
		
		for (Map objmap : objmaps) {
			itemElement = itemsElement.addElement(XmlRootNM);
			
			try {
				beaninfo = Introspector.getBeanInfo(clazz);
			} catch (IntrospectionException e1) {
				e1.printStackTrace();
			}
			
			PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
			if (propertyDescriptors != null && propertyDescriptors.length > 0) {
				for (PropertyDescriptor pd : propertyDescriptors) {
					if (!(pd.getName().equalsIgnoreCase("class") || pd.getName().equalsIgnoreCase("project_Id"))) {
						element = itemElement.addElement(pd.getName());
						Object obj = objmap.get(pd.getName());
						obj = obj == null ? "" : obj;
						element.setText(obj.toString());
					}
				}
			}
		}
		
		return convertDocToXMLStr(iscdate, document);
	}
	
	

	/**
	 * @Title: createXMLWithLineItem 
	 * @Description: 创建带一个行项页签的BPRecord
	 * @param @param iscdate
	 * @param @param XmlRootNM
	 * @param @param clazz1
	 * @param @param objmap
	 * @param @param clazz2
	 * @param @param objmaps
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static String createXMLWithLineItem(boolean iscdate, String XmlRootNM, Class clazzX, Map objmap, Class clazz1, List<Map> objmaps) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = itemsElement.addElement(XmlRootNM);
		Element element = null;
		BeanInfo beaninfo = null;

		try {
			beaninfo = Introspector.getBeanInfo(clazzX);
		} catch (IntrospectionException e1) {
			e1.printStackTrace();
		}

		PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
		if (propertyDescriptors != null && propertyDescriptors.length > 0) {
			for (PropertyDescriptor pd : propertyDescriptors) {
				if (!pd.getName().contentEquals("class")) {
					element = itemElement.addElement(pd.getName());
					Object obj = objmap.get(pd.getName());
					obj = obj == null ? "" : obj;
					element.setText(obj.toString());
				}
			}
			createXMLineItem(itemElement, clazz1, objmaps);
		}
		return convertDocToXMLStr(iscdate, document);
	}
	
	
	/**
	 * @Title: createXMLWithLineItemX2 
	 * @Description: 创建带两个行项页签的BPRecord
	 * @param @param iscdate
	 * @param @param XmlRootNM
	 * @param @param clazzX
	 * @param @param objmap
	 * @param @param clazz1
	 * @param @param objmaps1
	 * @param @param clazz2
	 * @param @param objmaps2
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static String createXMLWithLineItemX2(boolean iscdate, String XmlRootNM, Class clazzX, Map objmap, 
			Class clazz1, List<Map> objmaps1, Class clazz2, List<Map> objmaps2) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = itemsElement.addElement(XmlRootNM);
		Element element = null;
		BeanInfo beaninfo = null;

		try {
			beaninfo = Introspector.getBeanInfo(clazzX);
		} catch (IntrospectionException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
		if (propertyDescriptors != null && propertyDescriptors.length > 0) {
			for (PropertyDescriptor pd : propertyDescriptors) {
				if (!pd.getName().contentEquals("class")) {
					element = itemElement.addElement(pd.getName());
					Object obj = objmap.get(pd.getName());
					obj = obj == null ? "" : obj;
					element.setText(obj.toString());
				}
			}
			//DIFFERENT
//			createXMLineItem(itemElement, clazz1, objmaps1);
			createXMLineItem(itemElement, clazz2, objmaps2);
		}
		return convertDocToXMLStr(iscdate, document);
	}
	
	/**
	 * @Title: createXMLWithLineItemX3 
	 * @Description: 创建带三个行项页签的BPRecord
	 * @param @param iscdate
	 * @param @param XmlRootNM
	 * @param @param clazzX 上部类
	 * @param @param objmap 
	 * @param @param clazz1 行项页签1类
	 * @param @param objmaps1
	 * @param @param clazz2 行项页签2类
	 * @param @param objmaps2
	 * @param @param clazz3 行项页签3类
	 * @param @param objmaps3
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static String createXMLWithLineItemX3(boolean iscdate, String XmlRootNM, Class clazzX, Map objmap, 
			Class clazz1, List<Map> objmaps1, 
			Class clazz2, List<Map> objmaps2, 
			Class clazz3, List<Map> objmaps3) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = itemsElement.addElement(XmlRootNM);
		Element element = null;
		BeanInfo beaninfo = null;

		try {
			beaninfo = Introspector.getBeanInfo(clazzX);
		} catch (IntrospectionException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
		if (propertyDescriptors != null && propertyDescriptors.length > 0) {
			for (PropertyDescriptor pd : propertyDescriptors) {
				if (!pd.getName().contentEquals("class")) {
					element = itemElement.addElement(pd.getName());
					Object obj = objmap.get(pd.getName());
					obj = obj == null ? "" : obj;
					element.setText(obj.toString());
				}
			}
			//DIFFERENT
			createXMLineItem(itemElement, clazz1, objmaps1);
			createXMLineItem(itemElement, clazz2, objmaps2);
			createXMLineItem(itemElement, clazz3, objmaps3);
		}
		return convertDocToXMLStr(iscdate, document);
	}
	
	/**
	 * @Title: createXMLWithLineItemX4 
	 * @Description: 创建带四个行项页签的BPRecord
	 * @param @param iscdate
	 * @param @param XmlRootNM
	 * @param @param clazzX 上部类
	 * @param @param objmap
	 * @param @param clazz1 行项页签1类
	 * @param @param objmaps1
	 * @param @param clazz2 行项页签2类
	 * @param @param objmaps2
	 * @param @param clazz3 行项页签3类
	 * @param @param objmaps3
	 * @param @param clazz4 行项页签4类
	 * @param @param objmaps4
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static String createXMLWithLineItemX4(boolean iscdate, String XmlRootNM, Class clazzX, Map<String, String> objmap, 
			Class clazz1, List<Map> objmaps1, 
			Class clazz2, List<Map> objmaps2, 
			Class clazz3, List<Map> objmaps3, 
			Class clazz4, List<Map> objmaps4) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = itemsElement.addElement(XmlRootNM);
		Element element = null;
		BeanInfo beaninfo = null;

		try {
			beaninfo = Introspector.getBeanInfo(clazzX);
		} catch (IntrospectionException e1) {
			e1.printStackTrace();
		}

		PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
		if (propertyDescriptors != null && propertyDescriptors.length > 0) {
			for (PropertyDescriptor pd : propertyDescriptors) {
				if (!pd.getName().contentEquals("class")) {
					element = itemElement.addElement(pd.getName());
					Object obj = objmap.get(pd.getName());
					obj = obj == null ? "" : obj;
					element.setText(obj.toString());
				}
			}
			
			// 调用方法创建行项页签XML
			createXMLineItem(itemElement, clazz1, objmaps1);
			createXMLineItem(itemElement, clazz2, objmaps2);
			createXMLineItem(itemElement, clazz3, objmaps3);
			createXMLineItem(itemElement, clazz4, objmaps4);
		}
		return convertDocToXMLStr(iscdate, document);
	}
	/**
	 * @Title: createXMLWithLineItemX4 
	 * @Description: 创建带四个行项页签的BPRecord
	 * @param @param iscdate
	 * @param @param XmlRootNM
	 * @param @param clazzX 上部类
	 * @param @param objmap
	 * @param @param clazz1 行项页签1类
	 * @param @param objmaps1
	 * @param @param clazz2 行项页签2类
	 * @param @param objmaps2
	 * @param @param clazz3 行项页签3类
	 * @param @param objmaps3
	 * @param @param clazz4 行项页签4类
	 * @param @param objmaps4
	 * @param @param clazz5 行项页签5类
	 * @param @param objmaps5
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static String createXMLWithLineItemX5(boolean iscdate, String XmlRootNM, Class clazzX, Map objmap, 
			Class clazz1, List<Map> objmaps1, 
			Class clazz2, List<Map> objmaps2, 
			Class clazz3, List<Map> objmaps3, 
			Class clazz4, List<Map> objmaps4, 
			Class clazz5, List<Map> objmaps5) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = itemsElement.addElement(XmlRootNM);
		Element element = null;
		BeanInfo beaninfo = null;

		try {
			beaninfo = Introspector.getBeanInfo(clazzX);
		} catch (IntrospectionException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
		if (propertyDescriptors != null && propertyDescriptors.length > 0) {
			for (PropertyDescriptor pd : propertyDescriptors) {
				if (!pd.getName().contentEquals("class")) {
					element = itemElement.addElement(pd.getName());
					Object obj = objmap.get(pd.getName());
					obj = obj == null ? "" : obj;
					element.setText(obj.toString());
				}
			}
			
			// 调用方法创建行项页签XML
			createXMLineItem(itemElement, clazz1, objmaps1);
			createXMLineItem(itemElement, clazz2, objmaps2);
			createXMLineItem(itemElement, clazz3, objmaps3);
			createXMLineItem(itemElement, clazz4, objmaps4);
			createXMLineItem(itemElement, clazz5, objmaps5);
		}
		return convertDocToXMLStr(iscdate, document);
	}
	public static String createXMLWithLineItemX6(boolean iscdate, String XmlRootNM, Class clazzX, Map objmap, 
			Class clazz1, List<Map> objmaps1, 
			Class clazz2, List<Map> objmaps2, 
			Class clazz3, List<Map> objmaps3, 
			Class clazz4, List<Map> objmaps4, 
			Class clazz5, List<Map> objmaps5
			,Class clazz6, List<Map> objmaps6) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = itemsElement.addElement(XmlRootNM);
		Element element = null;
		BeanInfo beaninfo = null;

		try {
			beaninfo = Introspector.getBeanInfo(clazzX);
		} catch (IntrospectionException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
		if (propertyDescriptors != null && propertyDescriptors.length > 0) {
			for (PropertyDescriptor pd : propertyDescriptors) {
				if (!pd.getName().contentEquals("class")) {
					element = itemElement.addElement(pd.getName());
					Object obj = objmap.get(pd.getName());
					obj = obj == null ? "" : obj;
					element.setText(obj.toString());
				}
			}
			
			// 调用方法创建行项页签XML
			createXMLineItem(itemElement, clazz1, objmaps1);
			createXMLineItem(itemElement, clazz2, objmaps2);
			createXMLineItem(itemElement, clazz3, objmaps3);
			createXMLineItem(itemElement, clazz4, objmaps4);
			createXMLineItem(itemElement, clazz5, objmaps5);
			createXMLineItem(itemElement, clazz6, objmaps6);
		}
		return convertDocToXMLStr(iscdate, document);
	}
	
	public static String createXMLWithLineItemX7(boolean iscdate, String XmlRootNM, Class clazzX, Map objmap, 
			Class clazz1, List<Map> objmaps1, 
			Class clazz2, List<Map> objmaps2, 
			Class clazz3, List<Map> objmaps3, 
			Class clazz4, List<Map> objmaps4, 
			Class clazz5, List<Map> objmaps5,
			Class clazz6, List<Map> objmaps6,
			Class clazz7, List<Map> objmaps7) {
		 
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = itemsElement.addElement(XmlRootNM);
		Element element = null;
		BeanInfo beaninfo = null;

		try {
			beaninfo = Introspector.getBeanInfo(clazzX);
		} catch (IntrospectionException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
		if (propertyDescriptors != null && propertyDescriptors.length > 0) {
			for (PropertyDescriptor pd : propertyDescriptors) {
				if (!pd.getName().contentEquals("class")) {
					element = itemElement.addElement(pd.getName());
					Object obj = objmap.get(pd.getName());
					obj = obj == null ? "" : obj;
					element.setText(obj.toString());
				}
			}
			
			// 调用方法创建行项页签XML
			createXMLineItem(itemElement, clazz1, objmaps1);
			createXMLineItem(itemElement, clazz2, objmaps2);
			createXMLineItem(itemElement, clazz3, objmaps3);
			createXMLineItem(itemElement, clazz4, objmaps4);
			createXMLineItem(itemElement, clazz5, objmaps5);
			createXMLineItem(itemElement, clazz6, objmaps6);
		}
		return convertDocToXMLStr(iscdate, document);
	}

	/**
	 * @Title: createXMLineItem 
	 * @Description: 创建带1个行项的BP解析的XML
	 * @param @param ltElement
	 * @param @param clazz 上部类
	 * @param @param objmaps
	 * @param @return 
	 * @return String
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	private static String createXMLineItem(Element ltElement, Class clazz, List<Map> objmaps) {
		if(objmaps==null) {
			return null;
		}else {
			if(objmaps.size()>0) {
				Element itemElement = null;
				Element element = null;
				BeanInfo beaninfo = null;

				try {
					beaninfo = Introspector.getBeanInfo(clazz);
				} catch (IntrospectionException e1) {
					e1.printStackTrace();
				}

				PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
				for (Map<String, String> objmap : objmaps) {
					itemElement = ltElement.addElement(XMLHeaderConstants.LINEITEM);
					if (propertyDescriptors != null && propertyDescriptors.length > 0) {
						for (PropertyDescriptor pd : propertyDescriptors) {
							if (!pd.getName().contentEquals("class")) {
								element = itemElement.addElement(pd.getName());
								Object obj = objmap.get(pd.getName());
								obj = obj == null ? "" : obj;
								element.setText(obj.toString());
							}
						}
					}
				}
				return element.asXML();
			}else {
				return null;
			}
		}
	}
	
	
	/**
	 * @Title: createXMLFromList 
	 * @Description: TODO
	 * @param @param iscdate 是否待Cdate格式，默认fales
	 * @param @param XmlRootNM
	 * @param @param clazz
	 * @param @param objmaps
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static String createXMLFromList(boolean iscdate, String XmlRootNM, Class clazz, List<Map> objmaps) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = null;
		Element element = null;
		BeanInfo beaninfo = null;

		try {
			beaninfo = Introspector.getBeanInfo(clazz);
		} catch (IntrospectionException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		PropertyDescriptor[] propertyDescriptors = beaninfo.getPropertyDescriptors();
		for (Map objmap : objmaps) {
			// logger.info("record_no: "+objmap.get("record_no"));
			itemElement = itemsElement.addElement(XmlRootNM);
			if (propertyDescriptors != null && propertyDescriptors.length > 0) {
				for (PropertyDescriptor pd : propertyDescriptors) {
					if (!pd.getName().contentEquals("class")) {
						element = itemElement.addElement(pd.getName());
						Object obj = objmap.get(pd.getName());
						obj = obj == null ? "" : obj;
						element.setText(obj.toString());
					}
				}
			}
		}

		return convertDocToXMLStr(iscdate, document);
	}

	/**
	 * Demo how to create new cdata xml
	 * @param length
	 * @return
	 */
	public static String createXMLInCondition(boolean iscdate, String XmlRootNM, Map<String, String> map) {
		Document document = DocumentHelper.createDocument();
		Element itemsElement = document.addElement("List_Wrapper");
		Element itemElement = itemsElement.addElement(XmlRootNM);
		Element element = null;

		for (Map.Entry<String, String> entry : map.entrySet()) {
			element = itemElement.addElement(entry.getKey());
			element.setText(entry.getValue());
		}

		return convertDocToXMLStr(iscdate, document);
	}

	private static String convertDocToXMLStr(boolean iscdate, Document document) {
		 OutputFormat outputFormat = OutputFormat.createPrettyPrint();
		 outputFormat.setEncoding("UTF-8");
		 outputFormat.setSuppressDeclaration(true);
		 outputFormat.setIndent(false);
		 outputFormat.setIndent("");
		 outputFormat.setNewlines(false);

		String result = document.asXML();
		// System.out.println("result: " + result);
		return result.substring(38, result.length());

		// try {
		// StringWriter stringWriter = new StringWriter();
		// XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat);
		//
		// if (iscdate) {
		// stringWriter.write("<![CDATA[");
		// xmlWriter.write(document);
		// stringWriter.write("]]>");
		// } else {
		// xmlWriter.write(document);
		// }
		// xmlWriter.close();
		//
		//// logger.info("XX: "+stringWriter.toString());
		// return stringWriter.toString();
		// } catch (IOException e) {
		// e.printStackTrace();
		// return null;
		// }
	}

	/**
	 * demo how to create new cdata xml (or define)
	 * 
	 * @param length
	 * @return
	 */
	public static String createxmltemp(int length) {
		// 创建XML文档树  
		Document document = DocumentHelper.createDocument();
		// 创建根节点items  
		Element itemsElement = document.addElement("items");
		// 创建根节点下的item子节点
		Element itemElement = itemsElement.addElement("item");
		// item节点有两个子节点

		Element element = null;

		for (int i = 1; i < length + 1; i++) {
			element = itemElement.addElement("conn" + i);
			element.setText("10003" + i);
		}
		// 设置XML文档格式
		OutputFormat outputFormat = OutputFormat.createPrettyPrint();
		// 设置XML编码方式,即是用指定的编码方式保存XML文档到字符串(String),这里也可以指定为GBK或是ISO8859-1  
		outputFormat.setEncoding("UTF-8");
		outputFormat.setSuppressDeclaration(true); // 是否生产xml头
		outputFormat.setIndent(true); // 设置是否缩进
		outputFormat.setIndent("    "); // 以四个空格方式实现缩进
		outputFormat.setNewlines(true); // 设置是否换行

		try {
			// stringWriter字符串是用来保存XML文档的  
			StringWriter stringWriter = new StringWriter();
			// xmlWriter是用来把XML文档写入字符串的(工具)
			XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat);

			stringWriter.write("<![CDATA[");
			// 把创建好的XML文档写入字符串
			xmlWriter.write(document);
			stringWriter.write("]]>");
			xmlWriter.close();

			// 打印字符串,即是XML文档
			return stringWriter.toString();

		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 *  创建xml文件（带对象）from HEYUN
	 * @param list
	 * @param bpvo
	 * @return 
	 */
	@SuppressWarnings("unchecked")
	public static String objectConvertDom(Object object, Class clazz) {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("List_Wrapper");

		// 获取类中所有的字段
		Field[] fields = clazz.getDeclaredFields();

		// 先把List<T>对象转成json字符串
		String str = JSONArray.fromObject(object).toString();

		JSONArray json = new JSONArray();
		json = JSONArray.fromObject(str);

		// 把json字符串转换成List<Map<Object, Object>>
		List<Map<Object, Object>> mapList = (List<Map<Object, Object>>) json;
		Element element;
		Map<Object, Object> map;
		// 迭代拼接xml节点数据
		for (int i = 0; i < mapList.size(); i++) {
			// 在根节点下添加子节点
			element = root.addElement("_bp");

			// 获取Map<Object, Object>对象
			map = mapList.get(i);

			// 从map中获取数据，拼接xml
			for (Field field : fields) {
				// 在子节点下再添加子节点
				// BP记录编号不用生成，系统自动生成
				if (!field.getName().equalsIgnoreCase("record_no")) {
					element.addElement(field.getName()).addAttribute("attr", field.getType().getName()).addText(String.valueOf(map.get(field.getName())));
				}
			}
		}
		return document.asXML();
	}

	public static String strx(String date) {
		return "'" + date + "'";
	}
}